import java.awt.*;
import java.io.*;

class CStair extends CObject
{
	Anchor[] a;						// start & end points


	CStair(int type,int subtype,int x,int y1,int y2)
	{
		super(type,subtype);
		a = new Anchor[2];
		a[0] = new Anchor(x,y1);
		a[1] = new Anchor(x,y2);
		init();
	}

	void init()
	{
		ymin = (a[0].y < a[1].y) ? a[0].y : a[1].y;
		ymax = (a[0].y > a[1].y) ? a[0].y : a[1].y;
		x1 = x2 = a[0].x;
		y1 = ymin;
		y2 = ymax;
	}

	boolean checkActive(int mx,int my)
	{
		active = false;
	    if ((mx >= (a[0].x - 5)) && (mx <= (a[0].x + 5)) && (my >= ymin) && (my <= ymax))
	    {
	    	a[0].active = ((a[0].y + 6 >= my) && (a[0].y - 6 <= my));
	    	a[1].active = ((a[1].y + 6 >= my) && (a[1].y - 6 <= my));
	    	active = true;
	    }
//	    active = active || a[0].checkActive(mx,my);
//	    active = active || a[1].checkActive(mx,my);
	    return active;
	}

	void draw(Graphics g)
	{
		boolean mode = (type == MainFrame.activeType);
		g.setColor(selected ? Color.red : (mode ? color:color2));
		if (blink || !active || selected)
		{
			g.drawLine(a[0].x,a[0].y,a[1].x,a[1].y);
			a[0].draw(g,mode);
			a[1].draw(g,mode);
		}
		blink = !blink;
	}

	void move(int mx,int my,int start_x,int start_y)
	{
		if (a[0].active) moveAnchor(0,mx,my);
		else
		if (a[1].active) moveAnchor(1,mx,my);
		else
		{
			a[0].move(mx - start_x,my - start_y);
			a[1].move(mx - start_x,my - start_y);
		}
		init();
	}

	void moveAnchor(int index,int mx,int my)
	{
   		a[index].y = my;
		init();
	}

	void write(DataOutputStream out) throws IOException
	{
		out.writeByte((byte)type);
		out.writeByte((byte)subtype);
		out.writeShort((short)a[0].x);
		out.writeShort((short)a[0].y);
		out.writeShort((short)a[1].y);
	}
	static CObject read(DataInputStream in,int type) throws IOException
	{
		int subtype = in.readByte();
		int x1 = in.readShort();
		int y1 = in.readShort();
		int y2 = in.readShort();
		return new CStair(type,subtype,x1,y1,y2);
	}
}                
